package com.freewater.springboot.demo.ioc.lifecycle;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
public class Task implements Runnable {

    final private int num;

    private final Random random = new Random();

    public Task(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    @Override
    public void run() {
        final int waitSec = random.nextInt(5);
        try {
            log.info("thread {} task {} sleeping {} 秒", Thread.currentThread().threadId(), this.num, waitSec);
            TimeUnit.SECONDS.sleep(waitSec);
            log.info("thread {} task {} sleep completed", Thread.currentThread().threadId(), this.num);
        } catch (InterruptedException e) {
            log.warn("thread {} task {} was intercepted", Thread.currentThread().threadId(), this.num);
        }
    }
}
